package array;

import org.omg.CORBA.PUBLIC_MEMBER;

public class MyArray<E> {

    private E[] data;
    private int size;   //有效元素

    public MyArray(int capacity){
        data = (E[]) new Object[capacity];
        size = 0;
    }

    public MyArray(){
        this(10);
    }

    //查询
    public int getSize(){
        return size;
    }

    //获取数组容量
    public int getCapacity(){
        return data.length;
    }

    //判断数组是否为0
    public boolean isEmpty(){
        return size == 0;
    }

    //添加元素到末尾
    public void addLast(E e){
       add(size,e);
    }

    public void addFirst(E e){
        add(0,e);
    }

    /**
     * 插入元素到指定位置
     *
     * @param index:要插入的位置
     * @param e:待插入的元素
     */
    public void add(int index,E e){
        if(size == data.length){
            resize(2*data.length);
        }


        if(index < 0 || index > size){
            throw new IllegalArgumentException("Add failed,Require index >=0 OR");
        }

        //size-1是实际元素的index，i >= index 作为循环条件
        for(int i = size-1; i >= index ; i--){
            data[i + 1] = data[i];
        }

        data[index] = e;
        size++;
    }

    //
    public E get(int index){
        if(index < 0 || index >= size){
            throw new IllegalArgumentException("");
        }
        return data[index];
    }

    //获取数组中最后一个元素
    public E getLast(){
        int index = size - 1;
        return get(index);
    }

    //获取数组中第一个元素
    public E getFirst(){
        return get(0);
    }

    public void set(int index,E e){
        data[index] = e;
    }

    public boolean contains(E e){
        for (int i = 0; i < size; i++) {
            if(data[i].equals(e)){
                return true;
            }
        }

        return false;
    }

    public int find(E e){
        for (int i = 0; i < size; i++) {
            if(data[i].equals(e)){
                return i;
            }
        }
        return -1;
    }

    //从数组中删除元素
    public E remove(int index){
        if(index < 0 || index >= size){
            throw new IllegalArgumentException("");
        }

        E temp = data[index];

        for (int i = index + 1; i < size; i++) {
            data[i-1] = data[i];
        }

        size--;

        //如果当前元素个数小到整个数组容量的1/4的时候
        //使用Lazy测率，防止时间复杂度震荡
        if(size == data.length/4 && data.length/2 != 0){
            resize(data.length / 2);
        }

        return temp;
    }

    public E removeFirst(){
        return remove(0);
    }

    public E removeLast(){
        return remove(size-1);
    }

    public void removeElement(E e){
        int index = find(e);
        if(index != -1){
            remove(index);
        }
    }


    /**
     * 交换
     * @param i
     * @param j
     */
    public void swap(int i,int j){
        if(i < 0 || i >= size || j < 0 || j >= size){
            throw new IllegalArgumentException("Index is illegal");
        }

        E t = data[i];
        data[i] = data[j];
        data[j] = t;

    }



    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(String.format("Array: size = %d,capacity = %d \n",size,data.length));
        sb.append("[");
        for (int i = 0 ; i < size ; i++){
            sb.append(data[i]);
            if(i != size-1){
                sb.append(",");
            }
        }
        sb.append("]");
        return sb.toString();
    }

    /**
     * 数组扩容
     * @param newCapacity:新的容量
     */
    private void resize(int newCapacity){
        E[] newData = (E[]) new Object[newCapacity];
        for (int i = 0; i < size ; i++) {
            newData[i] = data[i];
        }
        data = newData;
    }

    public MyArray(E[] arr){
        data = (E[]) new Object[arr.length];
        for (int i = 0; i < arr.length; i++) {
            data[i] = arr[i];
        }
        size = arr.length;
    }


}
